package br.com.ps.bombertournament.game.action;

import br.com.ps.bombertournament.game.BaseAction;
import br.com.ps.bombertournament.game.BaseGameObject;
import br.com.ps.bombertournament.game.WrapperMap;

/**
 * Move the game object the actual x and y point to another position.	
 * 
 * @author Rennan Stefan Boni - rennanboni@gmail.com
 */
public class TranslateAction extends BaseAction {

	private static final String Y_PROPERTY = "y";
	private static final String X_PROPERTY = "x";
	
	private Float calculateX;
	private Float calculateY;
	
	@Override
	public void create() {
		calculate();
	}

	@Override
	public void execute(BaseGameObject owner) {
		owner.setPosition(owner.getX() + calculateX, owner.getY() + calculateY);
		
		super.setExecuted(true);
	}

	@Override
	public void undo(BaseGameObject owner) {
		if (calculateX != null || calculateY != null) {
			calculate();
		}
		
		owner.setPosition(owner.getX() - calculateX, owner.getY() - calculateY);
	}
	
	/**
	 * Calculate the new x and y point to move the game object
	 */
	private void calculate() {
		WrapperMap parameters = super.getParameters();
		
		if (calculateX == null) {
			if (parameters.containsKey(X_PROPERTY)) {
				calculateX = parameters.getFloat(X_PROPERTY);
			} else {
				calculateX = (float) 0;
			}
		}
		
		if (calculateY == null) {
			if (parameters.containsKey(Y_PROPERTY)) {
				calculateY = parameters.getFloat(Y_PROPERTY);
			} else {
				calculateY = (float) 0;
			}
		}
	}
}
